/**
 * 测试wait函数回收子进程；
 * @author: sundx
 * @date: 2020-05-30 22:05:14
 * @Last Modified by: sundx
 * @Last Modified time: 2020-05-30 22:05:36
 */
#include <fcntl.h>
#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
int main() {
  pid_t pid = fork();
  if (pid == -1) {
    std::cout << "fork error" << std::endl;
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    std::cout << "going to die " << getpid() << std::endl;
    sleep(2);
    return 23;
  } else {
    int status;
    pid_t sonPid = wait(&status);
    if (WIFEXITED(status)) {
      std::cout << sonPid << "  " << WEXITSTATUS(status) << std::endl;
    } else if (WIFSIGNALED(status)) {
      std::cout << WTERMSIG(status) << std::endl;
    }
  }
  return 0;
}